<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset = "UTF-8">
    <script src="{{url_for('static',filename='js/jquery.min.js')}}"></script>
    <script src="{{url_for('static',filename='js/stockIncubator.js')}}?{{version}}"></script>
    <script src="{{url_for('static',filename='js/stockIncubatorUser.js')}}?{{version}}"></script>
    <script src="{{url_for('static',filename='js/wangEditor.js')}}"></script>
    <script src="{{url_for('static',filename='js/bootstrap.min.js')}}"></script>
    <script src="{{url_for('static',filename='js/jquery.dataTables.min.js')}}"></script>
    <link rel="stylesheet" href="{{url_for('static',filename='css/jquery.dataTables.min.css')}}">
    <link rel="stylesheet" href="{{url_for('static',filename='css/bootstrap.min.css')}}">
    <link rel="stylesheet"  href="{{url_for('static',filename='css/user_info_style.css')}}" >
    <style>
    .button{
        background-color: white;
        color: black;
        border: 2px solid rgb(68,113,188);
    }

    .button:hover {
        background-color: rgb(68,113,188);
        color: white;
    }

    .button[disabled] {
        opacity: 0.6;
        cursor: not-allowed;
    }

    </style>
    <style>
        .black_overlay{
            display: none;
            position: absolute;
            top: 0%;
            left: 0%;
            width: 100%;
            height: 100%;
            background-color: black;
            z-index:1001;
            -moz-opacity: 0.8;
            opacity:.80;
            filter: alpha(opacity=88);
        }
        .white_content {
            display: none;
            position: absolute;
            top: 5%;
            left: 10%;
            width: 80%;
            height: 90%;
            padding: 20px;
            border: 4px solid rgb(68,113,188);
            background-color: white;
            z-index:1002;
            overflow: auto;
        }
    </style>
    <title>潜伏者</title>
</head>
<body>
<nav class="navbar navbar-default" role="navigation">
    <div class="container-fluid">
    <div class="navbar-header">
        <a class="navbar-brand" href="#">盯盘宝后台管理系统</a>
    </div>
    <div>
        <ul class="nav navbar-nav">
            <li ><a href="{{ url_for('start_web') }}">首页</a></li>
            <li ><a href="{{ url_for('permission_manage') }}">权限配置</a></li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                    管理员{{ session['admin'] if session['admin'] else "未登录" }}
                    <b class="caret"></b>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="{{ url_for("login_out") }}">退出</a></li>
                </ul>
            </li>
        </ul>
    </div>
    </div>
</nav>
<div id="light" class="white_content">
              <div class="form-group">
                  <label class="col-md-2">用户名</label>
                  <div class="col-md-10">
                    <input type="text" class="form-control" id="user-name">
                  </div>
              </div>
            <br/>
            <br/>
              <div class="form-group">
                  <label class="col-md-2">密码</label>
                  <div class="col-md-10">
                    <input type="text" class="form-control" id="password">
                  </div>
              </div>
            <br/>
            <br/>
              <div class="form-group">
                  <label class="col-md-2">注册时间</label>
                  <div class="col-md-10">
                    <input type="text" class="form-control" id="registration-time">
                  </div>
              </div>
            <br/>
            <br/>
              <div class="form-group">
                  <label class="col-md-2">邀请人</label>
                  <div class="col-md-10">
                    <input type="text" class="form-control" id="inviter">
                  </div>
              </div>
            <br/>
            <br/>
              <div class="form-group">
                  <label class="col-md-2">联系方式</label>
                  <div class="col-md-10">
                    <input type="text" class="form-control" id="contact-way">
                  </div>
              </div>
            <br/>
            <br/>
              <div class="form-group">
                  <label class="col-md-2">客户信息(必写)</label>
                  <div class="col-md-10">
                    <input type="text" class="form-control" id="client-info">
                  </div>
              </div>
            <br/>
            <br/>
              <div class="form-group">
                  <label class="col-md-2">用户类型</label>
                  <div class="col-md-10">
{#                    <input type="text" >#}
                      <select class="form-control" id="user-type">
                          <option>未使用</option>
                          <option>免费试用</option>
                          <option>测试账号</option>
                          <option>付费用户</option>
                      </select>
                  </div>
              </div>
            <br/>
            <br/>
              <div class="form-group">
                  <label class="col-md-2">付费金额</label>
                  <div class="col-md-10">
                    <input type="text" class="form-control" id="pay-money">
                  </div>
              </div>
            <br/>
            <br/>
              <div class="form-group">
                  <label class="col-md-2">邀请人数</label>
                  <div class="col-md-10">
                    <input type="text" class="form-control" id="invite-people-num">
                  </div>
              </div>
            <br/>
            <br/>
              <div class="form-group">
                  <label class="col-md-2">付费人数</label>
                  <div class="col-md-10">
                    <input type="text" class="form-control" id="pay-people-num">
                  </div>
              </div>
            <br/>
            <br/>
              <div class="form-group">
                  <label class="col-md-2">奖励金额</label>
                  <div class="col-md-10">
                    <input type="text" class="form-control" id="reward-money">
                  </div>
              </div>
            <br/>
            <br/>
              <div class="form-group">
                  <label class="col-md-2">奖励周期</label>
                  <div class="col-md-10">
                    <input type="text" class="form-control" id="reward-period">
                  </div>
              </div>
            <br/>
            <br/>
              <div class="form-group">
                  <label class="col-md-2">奖励备注</label>
                  <div class="col-md-10">
                    <input type="text" class="form-control" id="reward-remark">
                  </div>
              </div>
            <br/>
            <br/>
              <div class="form-group">
                  <label class="col-md-2">剩余天数</label>
                  <div class="col-md-10">
                    <input type="text" class="form-control" id="remaining_times">
                  </div>
              </div>
            <br/>
            <br/>
<div align="center">

    <div class="btn-group btn-group-lg" role="group" aria-label="...">


        <div class="btn-group" role="group">
        <button type="button" class="btn btn-primary" onclick="editor_user_submit()">提交</button>
        </div>

        <div class="btn-group" role="group">
        <button type="button" class="btn btn-primary" onclick="editor_user_cancel()">取消</button>
        </div>

    </div>
</div>

        </div>
<div id="fade" class="black_overlay"></div>
<div id="light-check" class="white_content">
<table id="userInfoDataTable" cellspacing="0" style="border: 1px solid rgb(68,113,188)" border="1"  width="80%">
        <thead>
        <tr>
        <th>Row</th>
        <th>客户信息</th>
        <th>联系方式</th>
        <th>购买时间</th>
        <th>购买股票</th>
        <th>买入时间</th>
        <th>卖出时间</th>
        <th>持仓成本</th>
        <th>现价</th>
        <th>今日涨跌幅(%)</th>
        <th>平仓成本</th>
        <th>交易状态</th>
        <th>剩余天数</th>
        </tr>
        </thead>
    </table>
<br/>
<br/>
<div align="center">

    <div class="btn-group btn-group-lg" role="group" aria-label="...">



        <div class="btn-group" role="group">
        <button type="button" class="btn btn-primary" onclick="editor_user_info_cancel()">关闭</button>
        </div>

    </div>
</div>

</div>
<div id="fade-check" class="black_overlay"></div>

<div align="center">
<div class="btn-group btn-group-lg" role="group" aria-label="...">
    <h3 align="center">潜伏者</h3>
    <br/>
    <br/>

        <div class="btn-group" >
          <button id="stock" type="button" class="btn btn-default" onclick="selectSource('stock')">股票管理</button>
          <button type="button" id="stock-toggle" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <span class="caret"></span>
            <span class="sr-only">Toggle Dropdown</span>
          </button>
          <ul class="dropdown-menu">
            <li><a href="#" onclick="selectSource('未上架')">未上架</a></li>
            <li><a href="#" onclick="selectSource('已上架')">已上架</a></li>
            <li><a href="#" onclick="selectSource('已下架')">已下架</a></li>
          </ul>
        </div>


        <div class="btn-group" role="group">
        <button id ="user" type="button" class="btn btn-default" onclick="changeDisplay('user')">客户管理</button>
        </div>

        <div class="btn-group" role="group">
        <button id ="trade" type="button" class="btn btn-default" onclick="changeDisplay('trade')">交易详情</button>
        </div>

        <div class="btn-group" role="group">
        <button id ="editor" type="button" class="btn btn-default" onclick="changeDisplay('editor')">弹窗编辑</button>
        </div>

        <div class="btn-group" role="group">
        <button id ="message" type="button" class="btn btn-default" onclick="changeDisplay('message')">历史消息</button>
        </div>

    </div>
</div>
<br/>

<div id="stock-manage">
<div class="row" align="center">
 <div class="btn-group" role="group" >
    <div class="btn-group" role="group">
    <button id ="free-user" type="button" class="btn btn-default" onclick="changeDisplayForStock('free-user')">普通用户</button>
    </div>
    <div class="btn-group" role="group">
    <button id ="pay-user" type="button" class="btn btn-default" onclick="changeDisplayForStock('pay-user')">付费用户</button>
    </div>
 </div>
</div>
    <br/>
    <br/>
    <div id="free-user-stock-manage">
<div id="stock-date" class="row" align="center" style="border:1px solid rgb(68, 113, 188);">
    <div id ="end_date_checkbox"  style="width:80%;">
    </div>
    <br/>
    <div align="center">
        <div class="btn-group btn-group-lg" role="group" aria-label="...">
        <div class="btn-group" role="group" align="center">
        <button type="button" class="btn btn-primary" onclick="selectSource('end_date')">查询</button>
        </div>
        <div class="btn-group" role="group" align="center">
        <button type="button" class="btn btn-primary" onclick="allIn(true)">全选</button>
        </div>
        <div class="btn-group" role="group" align="center">
        <button type="button" class="btn btn-primary" onclick="allIn(false)">全不选</button>
        </div>
        </div>
    </div>
    <br/>
</div>
<br/>

<div style="margin:auto">
<div id="add-block" align="center">
<div style="width:20%;">
 <div class="input-group">
     <input type="text" class="form-control" id="stock-input" />
     <span class="input-group-addon"><a href="#" onclick="addStock()"><i class="glyphicon glyphicon-plus"> <span>添加</span></i></a></span>
 </div>
</div>
</div>
<div id="profit"></div>
</div>
<div id="stockManageDataTable" class="table table-striped table-bordered" cellspacing="0" width="95%">
</div>
</div>

    <div id="pay-user-stock-manage" align="center">
        <div id="vip_checkbox" style="border:1px solid rgb(68, 113, 188);display: none;" align="center">


        </div>
        <br/>

<div style="width:20%;">
 <div class="input-group">
     <input type="text" class="form-control" id="stock-input-for-vip" />
     <span class="input-group-addon"><a href="#" onclick="addStockForVip()"><i class="glyphicon glyphicon-plus"> <span>添加</span></i></a></span>
 </div>
</div>

<table id="payUserStockDataTable" cellspacing="0" style="border: 1px solid #A3A3A3" border="1"  width="95%">
                <thead>
    <tr>
        <th>Row</th>
        <th>股票</th>
        <th>代码</th>
        <th>开始日期</th>
        <th>上架时间</th>
        <th>上架价格</th>
        <th>结束日期</th>
        <th>下架时间</th>
        <th>下架价格</th>
        <th>收益</th>
        <th>状态</th>
        <th>提醒价格下限</th>
        <th>提醒价格上限</th>
        <th>购买用户</th>
        <th>上架</th>
        <th>下架</th>
        <th>编辑</th>
    </tr>
    </thead>
        </table>
    </div>
</div>

<div id="client-manage" style="margin:auto; width:95%">
        <div class="row" align="center">
    <div class="btn-group" >
          <button id="user-type-button" type="button" class="btn btn-primary">全部</button>
          <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <span class="caret"></span>
            <span class="sr-only">Toggle Dropdown</span>
          </button>
          <ul class="dropdown-menu">
            <li><a href="#" onclick="selectSourceForUser('全部')">全部</a></li>
            <li><a href="#" onclick="selectSourceForUser('付费用户')">付费用户</a></li>
            <li><a href="#" onclick="selectSourceForUser('免费试用')">免费试用</a></li>
            <li><a href="#" onclick="selectSourceForUser('测试账号')">测试账号</a></li>
              <li><a href="#" onclick="selectSourceForUser('未使用')">未使用</a></li>
          </ul>
    </div>
    </div>
    <table id="userDataTable" cellspacing="0" style="border: 1px solid #A3A3A3" border="1"  width="95%">
    <thead>
    <tr>
        <th>Row</th>
        <th>用户名</th>
        <th>密码</th>
        <th>注册时间</th>
        <th>邀请人</th>
        <th>联系方式</th>
        <th>客户信息</th>
        <th>登录次数</th>
        <th>邀请码</th>
        <th>用户类型</th>
        <th>付费金额</th>
        <th>邀请人数</th>
        <th>付费人数</th>
        <th>奖励金额</th>
        <th>奖励周期</th>
        <th>奖励备注</th>
        <th>剩余天数</th>
        <th>账户状态</th>
        <th>交易详情</th>
        <th>编辑</th>
        <th>删除</th>
    </tr>
    </thead>
</table>
</div>

<div id="trade-detail" style="margin:auto; width:95%">

<div class="row" align="center">
 <div class="btn-group" role="group" >
    <div class="btn-group" role="group">
    <button id ="running" type="button" class="btn btn-default" onclick="changeDisplayForTradeDetail('running')">进行中</button>
    </div>
    <div class="btn-group" role="group">
    <button id ="over" type="button" class="btn btn-default" onclick="changeDisplayForTradeDetail('over')">已完成</button>
    </div>
 </div>
</div>
    <br/>
    <br/>
    <div id="tradeDetailRunningDataTableDiv">
    <table id="tradeDetailRunningDataTable" cellspacing="0" style="border: 1px solid #A3A3A3" border="1"  width="95%">

    <thead>
    <tr>
    <th>Row</th>
    <th>客户信息</th>
    <th>联系方式</th>
    <th>购买时间</th>
    <th>购买股票</th>
    <th>买入时间</th>
    <th>卖出时间</th>
    <th>持仓成本</th>
    <th>现价</th>
    <th>今日涨跌幅(%)</th>
    <th>平仓成本</th>
    <th>交易状态</th>
    <th>剩余天数</th>
    <th>编辑</th>
    </tr>
    </thead>
</table>
    </div>
    <div id="tradeDetailOverDataTableDiv">
    <table id="tradeDetailOverDataTable" cellspacing="0" style="border: 1px solid #A3A3A3" border="1"  width="95%">

    <thead>
    <tr>
    <th>Row</th>
    <th>客户信息</th>
    <th>联系方式</th>
    <th>购买时间</th>
    <th>购买股票</th>
    <th>买入时间</th>
    <th>卖出时间</th>
    <th>持仓成本</th>
    <th>现价</th>
    <th>今日涨跌幅(%)</th>
    <th>平仓成本</th>
    <th>交易状态</th>
    <th>剩余天数</th>
    <th>编辑</th>
    </tr>
    </thead>
</table>
    </div>
</div>

<div id="pop_up_window_editor" class="row" align="center">
    <div style="border:1px solid rgb(68, 113, 188);">
    <label><input type="checkbox" id="login_alert" value="login_alert">登录提醒</label>
    <label><input type="checkbox" id="info_alert" value="info_alert">资讯提醒</label>
    <label><input type="checkbox" id="popup_alert" value="popup_alert">弹窗提醒</label>
    <label><input type="checkbox" id="broadcasting" value="broadcasting">广播</label>
    </div>
    <br/><br/>
 <div class="btn-group" role="group">
    <div class="btn-group" role="group">
    <button id ="submit" type="button" class="btn btn-primary" onclick="submit(true)">提交</button>
    </div>

    <div class="btn-group" role="group">
    <button id ="clear" type="button" class="btn btn-primary" onclick="submit(false)">清空提示框</button>
    </div>

    <div class="btn-group" role="group">
    <button id ="clear_broadcast" type="button" class="btn btn-primary" onclick="submit('broadcast')">清空广播</button>
    </div>

     <div class="btn-group" role="group">
    <button id ="view_information" type="button" class="btn btn-primary" onclick="checkInfo()">查看资讯</button>
    </div>
 </div>
<br/>
<br/>

    <div style="width:20%;">
 <div class="input-group">
     <input type="text" class="form-control" id="info-title" />
     <span class="input-group-addon"> <span>标题</span></span>
 </div>
</div>
    <br/>
    <br/>
<div id="div1" style="width:60%;">
</div>
</div>

<div id="message_history" class="row" align="center">
<table id="messageHistoryDataTable" cellspacing="0" style="border: 1px solid rgb(68,113,188)" border="1"  width="80%">
        <thead>
        <tr>
            <th>Row</th>
            <th>提醒时间</th>
            <th>标题</th>
            <th>类型</th>
            <th>消息</th>
            <th>删除</th>
        </tr>
        </thead>
    </table>
</div>

<script>
    function selectSourceForUser(type) {
        $("#user-type-button").text(type);
             if (type == "全部"){
         type = "";
        }
        userDataTable.column(9).search(type).draw();
    }

    function change_account_status(account_id,account_status){
            $.ajax({
            url: "/changeAccountStatus",
            async: true,
            type: 'POST',
            data: JSON.stringify({'account_id':account_id,'account_status':account_status}),
            dataType: 'json',
            success: function (result) {
                var message = result.message;
                alert(message);
                refreshDataForTable();
            }
            });
    }

    function checkInfo() {
     window.open("http://10.0.0.3:8004/stockIncubatorInformation");
    }

    var E = window.wangEditor;
    var editor = new E('#div1');
    editor.create();
    var userInfoDataTable = null;

function addStock() {
    var stock = $("#stock-input").val();
    if (stock== ""){
        alert("请输入股票名");
        return;
    }
    $.ajax({
    url: "/updateStockIncubator",
    async: true,
    type: 'POST',
    data: JSON.stringify({'type':"add", 'stock_code': stock, 'begin_date': ""}),
    dataType: 'json',
    success: function (result) {
        var message = result.message;
        alert(message);
        refreshDataForTable();
    }
    });
}

function addStockForVip() {
    var stock = $("#stock-input-for-vip").val();
    if (stock== ""){
        alert("请输入股票名");
        return;
    }
    $.ajax({
    url: "/updateStockIncubatorForVip",
    async: true,
    type: 'POST',
    data: JSON.stringify({'type':"add", 'stock_code': stock}),
    dataType: 'json',
    success: function (result) {
        var message = result.message;
        alert(message);
        refreshDataForTable();
    }
    });
}

function becomeBold() {

    var text = $("#text_editor").html();
    const selection = window.getSelection(); //获取Selection对象
    var selectText = selection.toString(); //获取选中字符
    if (selectText==""){
        alert("请选择文字!");
        return;
    }
    var endIndex = selection.anchorOffset; //获取选中字符的开始
    var beginIndex = selection.focusOffset; //获取选中字符的结束
    var beginText = text.substring(0,beginIndex);
    var endText = text.substring(endIndex);
    var htmlStr = beginText + "<b>" + selectText + "</b>"+ endText;
    $("#text_editor").html(htmlStr);
}

function changeColor() {
    var text = $("#text_editor").html();
    const selection = window.getSelection(); //获取Selection对象
    var selectText = selection.toString(); //获取选中字符
    if (selectText==""){
        alert("请选择文字!");
        return;
    }
    var endIndex = selection.anchorOffset; //获取选中字符的开始
    var beginIndex = selection.focusOffset; //获取选中字符的结束
    var beginText = text.substring(0,beginIndex);
    var endText = text.substring(endIndex);
    if ($.type(color) == "undefined"){
        alert("请选择颜色!");
        return;
    }
    var htmlStr = beginText + "<font style=\"color:"+ color +"\">" + selectText + "</font>"+ endText;
    $("#text_editor").html(htmlStr);
}

function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = strDate+"/"+date.getHours() + seperator2 + date.getMinutes();
            //+ seperator2 + date.getSeconds();
    return currentdate;
}

function submit(flag) {
    if ($.type(flag) == 'string'){
        var htmlStr = "";
        var chooseTypeList = ["broadcasting"];
    }else{
        if (flag){
        var htmlStr = $("div[class=w-e-text]").html();
        var text = $("div[class=w-e-text]").find("p").html();

        if (text=="" | text=="<br>"){
            alert("请输入内容!");
            return;
        }
        var htmlStr = "<div><p>" +String(getNowFormatDate())+" " + text + "</p></div>";
        var typeList = ["login_alert","info_alert","popup_alert","broadcasting"];//ZB添加broadcasting
        var chooseTypeList = [];
        for (var i=0;i<typeList.length;i++){
        if($("#"+typeList[i]).is(':checked')){
            chooseTypeList.push(typeList[i]);
        }
        }
        if (chooseTypeList.length==0){
            alert("请选择提醒类型");
            return;
        }
        if ($("#info-title").val()==""){
        alert("请输入标题");
        return;
        }
    }
        else{
            var htmlStr = "";
            var chooseTypeList = ["login_alert"];
        }
    }

    $.ajax({
    url: "/stockIncubatorSubmitEditor",
    async: true,
    type: 'POST',
    data: JSON.stringify({'htmlStr':htmlStr,"chooseTypeList":chooseTypeList,
        "title":$("#info-title").val()}),
    dataType: 'json',
    success: function (result) {
        var message = result.message;
        alert(message);
    }
    });
}

function allIn(flag){
    if (flag){
          $(".end-date-checkbox").each(function (key,value) {
        $(value).prop("checked",true);
        var value = $(value).val();
        console.log(value);
        });
    }else{
        $(".end-date-checkbox").each(function (key,value) {
        $(value).prop("checked",false);
        var value = $(value).val();
        console.log(value);
        });
    }
}

function allInForVip(flag){
    if (flag){
          $(".pay-user-checkbox").each(function (key,value) {
        $(value).prop("checked",true);
        var value = $(value).val();
        console.log(value);
        });
    }else{
        $(".pay-user-checkbox").each(function (key,value) {
        $(value).prop("checked",false);
        var value = $(value).val();
        console.log(value);
        });
    }
}

function selectSource(type) {

    if (type=="stock"){
        $("#stock").text("股票管理");
        changeDisplay("stock");
        allIn(true);
        var filterResultList = resultList;
    }else if(type =="end_date"){
        var filterResultList = getSelectDate(resultList);
    }else{
        allIn(true);
        $("#stock").text(type);
        var filterResultList = resultList.filter(function (item) {
        return item["status"] == type;
        });
    }
    stockManageDataTable.data(filterResultList, 'code');
}

function getSelectDate(inputList) {
    var endDateList = [];
    $(".end-date-checkbox").each(function (key,value) {
        if ($(value).is(':checked')){
            var value = $(value).val();
            endDateList.push(value);
        }
    });
    var outputList=inputList.filter(
        function (item) {
            return (endDateList.indexOf(item["end_date"]) > -1);
        });
    return outputList;
}

function del_user(account_id,password) {
        $.ajax({
            url: "/updateStockIncubator",
            async: true,
            type: 'POST',
            data: JSON.stringify({'type':'del_user_password', 'account_id': account_id,
                'password': password}),
            dataType: 'json',
            success: function (result) {
                var message = result.message;
                alert(message);
                refreshDataForTable();
            }
        });
}

function editor_user(account_id,password){
    document.getElementById('light').style.display='block';
    document.getElementById('fade').style.display='block';
    $("#user-name").val(account_id);
    $("#user-name").attr("disabled","true");
    $("#password").val(password);
    $("#password").attr("disabled","true");
    for (var i=0;i<userList.length;i++){
        if (userList[i]["account_id"]==account_id){
            $("#registration-time").val(userList[i]["registration_time"]);
            $("#inviter").val(userList[i]["inviter"]);
            $("#contact-way").val(userList[i]["contact_way"]);
            $("#client-info").val(userList[i]["client_info"]);
            $("#user-type").val(userList[i]["user_type"]);
            $("#pay-money").val(userList[i]["pay_money"]);
            $("#invite-people-num").val(userList[i]["invite_people_num"]);
            $("#pay-people-num").val(userList[i]["pay_people_num"]);
            $("#reward-money").val(userList[i]["reward_money"]);
            $("#reward-period").val(userList[i]["reward_period"]);
            $("#reward-remark").val(userList[i]["reward_remark"]);
            $("#remaining_times").val(userList[i]["remaining_times"]);
        }
    }

    }

function editor_user_info(account_id,rowNum) {

        if ($(".user_info.button." + account_id+"."+rowNum).text() == "编辑"){
            $(".user_info.button." + account_id+"."+rowNum).text("提交");
            $(".user_info." + account_id+"."+rowNum).removeAttr("disabled");
        }else{
            $(".user_info.button." + account_id+"."+rowNum).text("编辑");
            $(".user_info." + account_id+"."+rowNum).attr("disabled",true);
            $(".user_info.button." + account_id+"."+rowNum).removeAttr("disabled");
            var stock_name = $(".user_info.stock_name." + account_id +"."+rowNum).val();
            var stock_code = $(".user_info.stock_code." + account_id +"."+rowNum).val();
            var attention_time = $(".user_info.attention_time." + account_id +"."+rowNum).val();
            var alert_price_low = $(".user_info.alert_price_low." + account_id +"."+rowNum).val();
            var alert_price_high = $(".user_info.alert_price_high." + account_id +"."+rowNum).val();
            var alert_status = $(".user_info.alert_status." + account_id +"."+rowNum).val();
            var trade_status = $(".user_info.trade_status." + account_id +"."+rowNum).val();
            var remaining_times = $(".user_info.remaining_times." + account_id +"."+rowNum).val();
            if (stock_code == ""){
                alert("请输入股票代码!");
                return;
            }
            $.ajax({
            url: "/updateStockIncubatorUserInfo",
            async: true,
            type: 'POST',
            data: JSON.stringify({'account_id': account_id,'stock_name':stock_name,'stock_code':stock_code,
                'attention_time':attention_time,'alert_price_low':alert_price_low,'alert_price_high':alert_price_high,
                'alert_status': alert_status,'trade_status':trade_status,'remaining_times':remaining_times}),
            dataType: 'json',
            success: function (result) {
                var message = result.message;
                alert(message);
            }
        });

        }

}

function editor_trade_detail(id) {

        if ($(".trade_detail.button." + id).text() == "编辑"){
            $(".trade_detail.button." + id).text("提交");
            $(".trade_detail." + id).removeAttr("disabled");
        }else{
            $(".trade_detail.button." + id).text("编辑");
            $(".trade_detail." + id).attr("disabled",true);
            $(".trade_detail.button." + id).removeAttr("disabled");


            var buy_time = $(".trade_detail.buy_time." + id).val();
            var first_cost = $(".trade_detail.first_cost." + id).val();
            var payback_cost = $(".trade_detail.payback_cost." + id).val();

            //if (parseInt(id) > userInfoList.length){
            //    var type = "add";
            //}else{
            //    var type = "update";
            //}

            var type = "update";
            $.ajax({
            url: "/updateStockIncubatorUserInfo",
            async: true,
            type: 'POST',
            data: JSON.stringify({'type':type,'id':id,'buy_time':buy_time,
                'first_cost': first_cost,
                'payback_cost':payback_cost}),
            dataType: 'json',
            success: function (result) {
                var message = result.message;
                alert(message);
                refreshDataForTable();
            }
        });

        }

}

function editor_stock_for_vip(stock_code,begin_date) {

        if ($(".stock_for_vip.button." + stock_code+"."+begin_date).text() == "编辑"){
            $(".stock_for_vip.button." + stock_code+"."+begin_date).text("提交");
            $(".stock_for_vip." + stock_code+"."+begin_date).removeAttr("disabled");
        }else{
            $(".stock_for_vip.button." + stock_code+"."+begin_date).text("编辑");
            $(".stock_for_vip." + stock_code+"."+begin_date).attr("disabled",true);
            $(".stock_for_vip.button." + stock_code+"."+begin_date).removeAttr("disabled");
            //var profit = $(".stock_for_vip.profit." + stock_code +"."+begin_date).val();
            var alert_price_low = $(".stock_for_vip.alert_price_low." + stock_code +"."+begin_date).val();
            var alert_price_high = $(".stock_for_vip.alert_price_high." + stock_code +"."+begin_date).val();

            if (stock_code == ""){
                alert("请输入股票代码!");
                return;
            }
            $.ajax({
            url: "/updateStockIncubatorForVip",
            async: true,
            type: 'POST',
            data: JSON.stringify({'type':"editor",'begin_date': begin_date,'stock_code':stock_code,
                'alert_price_low':alert_price_low,'alert_price_high':alert_price_high}),
            dataType: 'json',
            success: function (result) {
                var message = result.message;
                alert(message);
                refreshDataForTable();
            }
        });

        }

}

function editor_user_submit() {
    var user_name = $("#user-name").val();
    var password = $("#password").val();
    var registration_time = $("#registration-time").val();
    var inviter = $("#inviter").val();
    var contact_way = $("#contact-way").val();
    var client_info = $("#client-info").val();
    var user_type = $("#user-type").val();
    var pay_money = $("#pay-money").val();
    var invite_people_num = $("#invite-people-num").val();
    var pay_people_num = $("#pay-people-num").val();
    var reward_money = $("#reward-money").val();
    var reward_period = $("#reward-period").val();
    var reward_remark = $("#reward-remark").val();
    var remaining_times = $("#remaining_times").val();
    if (client_info == ""){
        alert("请输入客户信息!");
        return;
    }
    $.ajax({
            url: "/updateStockIncubator",
            async: true,
            type: 'POST',
            data: JSON.stringify({'type':'user_password', 'account_id': user_name,
                'password': password,'contact_way':contact_way,'client_info':client_info,
                'registration_time':registration_time,'inviter':inviter,'user_type':user_type,
                'pay_money':pay_money,'invite_people_num':invite_people_num,'pay_people_num':pay_people_num,
                'reward_money':reward_money,'reward_period':reward_period,'reward_remark':reward_remark,
                'remaining_times':remaining_times}),
            dataType: 'json',
            success: function (result) {
                var message = result.message;
                alert(message);
                document.getElementById('light').style.display='none';
                document.getElementById('fade').style.display='none';
                refreshDataForTable();
            }
        });
}

function editor_user_cancel() {
    document.getElementById('light').style.display='none';
    document.getElementById('fade').style.display='none';
}

function editor_user_info_cancel() {
    document.getElementById('light-check').style.display='none';
    document.getElementById('fade-check').style.display='none';
}

function check_detail(client_info) {
    document.getElementById('light-check').style.display='block';
    document.getElementById('fade-check').style.display='block';
    filterUserInfoList = [];
    for (var i=0;i<userInfoList.length;i++){
        if (userInfoList[i]["client_info"] == client_info){
            filterUserInfoList.push(userInfoList[i]);
        }
    }
    if (userInfoDataTable!=null){
            userInfoDataTable.destroy();
    }
    userInfoDataTable = $('#userInfoDataTable').DataTable( {
                "paging":false,
                "ordering": false,
                "info": false,
                "data": filterUserInfoList,
                "showRowNumber":true,
                "lengthChange": false,
                "destory":true,
                "columns": [
                    {"data":"id"},
                    {"data":"client_info"},
                    {"data":"contact_way"},
                    {"data":"buy_time"},
                    { "data": "stock_name" },
                    {"data":"put_away_time"},
                    {"data":"sold_out_time"},
                    { "data": "first_cost" },
                    {"data":"current_price"},
                    { "data": "change_ratio" },
                    {"data":"payback_cost"},
                    {"data":"trade_status"},
                    {"data":"remaining_times"}
                ]
            } );
}

function editor_user_info_add_row(){
    userInfoDataTable.row.add({
        "rowNum":filterUserInfoList.length+1,
        "account_id":userInfoAccountId,
        "stock_name":"",
        "stock_code":"",
        "attention_time":"",
         "alert_price_low" :"",
        "alert_price_high":"",
         "alert_status" :"",
        "trade_status":"",
        "remaining_times":""
    }).draw();
}

function update(type,stock_code,begin_date) {
    $.ajax({
        url: "/updateStockIncubator",
        async: true,
        type: 'POST',
        data: JSON.stringify({'type':type, 'stock_code': stock_code, 'begin_date': begin_date}),
        dataType: 'json',
        success: function (result) {
            var message = result.message;
            alert(message);
            refreshDataForTable();
        }
    });

}

function update_putaway_for_vip(type,stock_code,alert_price_low,alert_price_high,stock_name) {
    if (type=="put_away"){
        var htmlStr = "            <div id =\"pay_user_checkbox\"  style=\"width:80%;\" align=\"center\">\n" +
            "            </div>\n" +
            "            <br/>\n" +
            "        <div align=\"center\">\n" +
            "        <div class=\"btn-group btn-group-lg\" role=\"group\" aria-label=\"...\">\n" +
            "\n" +
            "        <div class=\"btn-group\" role=\"group\" align=\"center\">\n" +
            "        <button type=\"button\" class=\"btn btn-primary\" onclick=\"vip_put_away_submit(\'"+stock_code+"\',\'"+stock_name+"\',\'"+alert_price_low+"\',\'"+alert_price_high
            +"\')\">提交</button>\n" +
            "        </div>\n" +
            "\n" +
            "        <div class=\"btn-group\" role=\"group\" align=\"center\">\n" +
            "        <button type=\"button\" class=\"btn btn-primary\" onclick=\"allInForVip(true)\">全选</button>\n" +
            "        </div>\n" +
            "        <div class=\"btn-group\" role=\"group\" align=\"center\">\n" +
            "        <button type=\"button\" class=\"btn btn-primary\" onclick=\"allInForVip(false)\">全不选</button>\n" +
            "        </div>\n" +
            "        <div class=\"btn-group\" role=\"group\" align=\"center\">\n" +
            "        <button type=\"button\" class=\"btn btn-primary\" onclick=\"vip_put_away_cancel()\">取消</button>\n" +
            "        </div>\n" +
            "\n" +
            "        </div>\n" +
            "    </div>\n" +
            "            <br/>";

        $("#vip_checkbox").html(htmlStr);
        refreshPayUserCheckBox();
    }
}

function update_soldout_for_vip(type,stock_code,stock_name,put_away_price,begin_date) {
        $.ajax({
        url: "/updateStockIncubatorForVip",
        async: true,
        type: 'POST',
        data: JSON.stringify({'type':"sold_out", 'stock_code': stock_code,
            'stock_name':stock_name,'put_away_price':put_away_price,
            'begin_date': begin_date}),
        dataType: 'json',
        success: function (result) {
            var message = result.message;
            alert(message);
            refreshDataForTable();
        }
    });
}

function vip_put_away_submit(stock_code,stock_name,alert_price_low,alert_price_high){
    var accountList = [];
    $(".pay-user-checkbox").each(function (key,value) {
        if ($(value).is(':checked')){
            var value = $(value).val();
            accountList.push(value);
        }
    });
    if (accountList.length == 0){
        alert("请选择一个客户!")
        return;
    }
    $.ajax({
        url: "/updateStockIncubatorForVip",
        async: true,
        type: 'POST',
        data: JSON.stringify({'type':"put_away", 'stock_code': stock_code,
            'stock_name': stock_name,'account_list':accountList,
            'alert_price_low':alert_price_low,'alert_price_high':alert_price_high}),
        dataType: 'json',
        success: function (result) {
            var message = result.message;
            alert(message);
            refreshDataForTable();
        }
    });
}

function vip_put_away_cancel() {
    $("#vip_checkbox").hide();
}

var stockManageDataTable = null;
var resultList = [];
var userList = [];
var userInfoList = [];
var filterUserInfoList = [];
stockManageDataTable = jQuery("#stockManageDataTable").raytable({
            datasource: {data: [], keyfield: 'stock_code'},
            columns: [
                {field:"name",title:"股票"},
                {field:"code",title:"代码"},
                {field: "begin_date", title: "开始日期"},
                {field: "end_date", title: "结束日期"},
                {field: "profit", title: "收益(%,取当天最高点)"},
                {field: "status", title: "状态"},
                {field: "put_away", title: "上架"},
                {field: "sold_out", title: "下架"},
                {field: "del", title: "删除"}
            ],
            pagesize: 10,
            maxPageButtons: 5,
            rowNumbers: true
        });

var userInfoAccountId = "";
var payUserStockDataTable = null;
var tradeDetailRunningDataTable = null;
var tradeDetailOverDataTable = null;
var userDataTable = null;
var messageHistoryDataTable = null;
function getTradeDetailTable(type,inputData) {
    if (type == "running"){
        if (tradeDetailRunningDataTable !=null){
        tradeDetailRunningDataTable.destroy();
        }
        tradeDetailRunningDataTable = $('#tradeDetailRunningDataTable').DataTable({
            "ordering": false,
            "info":     false,
            "searching": false,
            "paging": false,
            "data": inputData,
            "lengthChange": false,
            "columns": [
                {"data":"row_num"},
                {"data":"client_info"},
                {"data":"contact_way"},
                {"data":"buy_time"},
                { "data": "stock_name" },
                {"data":"put_away_time"},
                {"data":"sold_out_time"},
                { "data": "first_cost" },
                {"data":"current_price"},
                { "data": "change_ratio" },
                {"data":"payback_cost"},
                {"data":"trade_status"},
                {"data":"remaining_times"},
                {"data":""}
            ],
            "columnDefs": [
            {
                "targets": 3,
                "render": function ( data, type, full, meta ) {
                    var id = full["id"];
                    var htmlStr = '<input style="width:80px" class="trade_detail buy_time '+ id +
                     '" type="text"  disabled=true  value="'+
                     data + '" />';
                    return htmlStr;
                }
            },
            {
                "targets": 7,
                "render": function ( data, type, full, meta ) {
                    var id = full["id"];
                    var htmlStr = '<input style="width:80px" class="trade_detail first_cost '+ id +
                     '" type="text"  disabled=true  value="'+
                     data + '" />';
                    return htmlStr;
                }
            },
            {
                "targets": 9,
                "render": function ( data, type, full, meta ) {
                    var id = full["id"];
                　　if (parseFloat(data).toString() == "NaN") {
                        var htmlStr = "<font>"+String(data)+"</font>";
                　　} else {
                　　　　if (parseFloat(data)>=0){
                            var htmlStr = "<font style='color:red'>"+String(data)+"</font>";
                        }else{
                            var htmlStr = "<font style='color:green'>"+String(data)+"</font>";
                    }
                　　}
                    return htmlStr;
                }
            },
            {
                "targets": 10,
                "render": function ( data, type, full, meta ) {
                    var id = full["id"];
                    var htmlStr = '<input style="width:80px" class="trade_detail payback_cost '+ id +
                     '" type="text"  disabled=true  value="'+
                     data + '" />';
                    return htmlStr;
                }
            },
            {
                "targets": -1,
                "render": function ( data, type, full, meta ) {
                    var id = full["id"];
                    var cell = '<button class="trade_detail button ' +  id +
                     '" type="text" onclick="editor_trade_detail(\''+ id + '\')" >编辑</button>';
                    return cell;
                }
            }
            ]
        });
    }
    if (type == "over"){
        if (tradeDetailOverDataTable !=null){
        tradeDetailOverDataTable.destroy();
        }
        tradeDetailOverDataTable = $('#tradeDetailOverDataTable').DataTable({
            "ordering": false,
            "info":     false,
            "data": inputData,
            "lengthChange": false,
            "columns": [
                {"data":"row_num"},
                {"data":"client_info"},
                {"data":"contact_way"},
                {"data":"buy_time"},
                { "data": "stock_name" },
                {"data":"put_away_time"},
                {"data":"sold_out_time"},
                { "data": "first_cost" },
                {"data":"current_price"},
                { "data": "change_ratio" },
                {"data":"payback_cost"},
                {"data":"trade_status"},
                {"data":"remaining_times"},
                {"data":""}
            ],
            "columnDefs": [
            {
                "targets": 3,
                "render": function ( data, type, full, meta ) {
                    var id = full["id"];
                    var htmlStr = '<input style="width:80px" class="trade_detail buy_time '+ id +
                     '" type="text"  disabled=true  value="'+
                     data + '" />';
                    return htmlStr;
                }
            },
            {
                "targets": 7,
                "render": function ( data, type, full, meta ) {
                    var id = full["id"];
                    var htmlStr = '<input style="width:80px" class="trade_detail first_cost '+ id +
                     '" type="text"  disabled=true  value="'+
                     data + '" />';
                    return htmlStr;
                }
            },
            {
                "targets": 9,
                "render": function ( data, type, full, meta ) {
                    var id = full["id"];
                　　if (parseFloat(data).toString() == "NaN") {
                        var htmlStr = "<font>"+String(data)+"</font>";
                　　} else {
                　　　　if (parseFloat(data)>=0){
                            var htmlStr = "<font style='color:red'>"+String(data)+"</font>";
                        }else{
                            var htmlStr = "<font style='color:green'>"+String(data)+"</font>";
                    }
                　　}
                    return htmlStr;
                }
            },
            {
                "targets": 10,
                "render": function ( data, type, full, meta ) {
                    var id = full["id"];
                    var htmlStr = '<input style="width:80px" class="trade_detail payback_cost '+ id +
                     '" type="text"  disabled=true  value="'+
                     data + '" />';
                    return htmlStr;
                }
            },
            {
                "targets": -1,
                "render": function ( data, type, full, meta ) {
                    var id = full["id"];
                    var cell = '<button class="trade_detail button ' +  id +
                     '" type="text" onclick="editor_trade_detail(\''+ id + '\')" >编辑</button>';
                    return cell;
                }
            }
            ]
        });
    }
}

function refreshData() {
    $.ajax({
        url: "/stockIncubatorData",
        async: true,
        success: function (result) {
            if (jQuery.type(result)=="string"){
                var obj = JSON.parse(result);
            }else{
                var obj = result;
            }
            resultList = obj.resultList;
            userList = obj.userList;
            userInfoList = obj.userInfoList;
            var runningUserInfoList = obj.runningUserInfoList;
            var overUserInfoList = obj.overUserInfoList;
            var totalProfit = obj.totalProfit;
            var messageList = obj.messageList;
            var vipList = obj.vipList;
            $("#profit").html("<h3 align='center'>历史总收益<b style='color:red;'>"+String(totalProfit)+"%</b></h3>");
            stockManageDataTable.data(resultList, 'stock_code');
            if (payUserStockDataTable !=null){
                payUserStockDataTable.destroy();
            }
            payUserStockDataTable = $('#payUserStockDataTable').DataTable({
                "ordering": false,
                "info":     false,
                "data": vipList,
                "lengthChange": false,
                "columns": [
                    {"data":"id"},
                    { "data": "name" },
                    { "data": "code" },
                    {"data":"begin_date"},
                    {"data":"begin_time"},
                    {"data":"put_away_price"},
                    {"data":"end_date"},
                    {"data":"end_time"},
                    {"data":"sold_out_price"},
                    { "data": "profit" },
                    {"data":"status"},
                    { "data": "alert_price_low" },
                    {"data":"alert_price_high"},
                    {"data":"client_info"},
                    {"data":""},
                    {"data":""},
                    {"data":""}
                ],
                "columnDefs": [
                    /*
                {
                    "targets": 7,
                    "render": function ( data, type, full, meta ) {
                        var stockCode = full["code"];
                        var beginDate = full["begin_date"];
                        var htmlStr = '<input style="width:80px" class="stock_for_vip profit '+ stockCode +' '+beginDate+
						 '" type="text"  disabled=true  value="'+
						 data + '" />';
                        return htmlStr;
                    }
                },*/
                {
                    "targets": 11,
                    "render": function ( data, type, full, meta ) {
                        var stockCode = full["code"];
                        var beginDate = full["begin_date"];
                        var htmlStr = '<input style="width:80px" class="stock_for_vip alert_price_low '+ stockCode +' '+beginDate+
						 '" type="text"  disabled=true  value="'+
						 data + '" />';
                        return htmlStr;
                    }
                },
                {
                    "targets": 12,
                    "render": function ( data, type, full, meta ) {
                        var stockCode = full["code"];
                        var beginDate = full["begin_date"];
                        var htmlStr = '<input style="width:80px" class="stock_for_vip alert_price_high '+ stockCode +' '+beginDate+
						 '" type="text"  disabled=true  value="'+
						 data + '" />';
                        return htmlStr;
                    }
                },
                {
                    "targets": -3,
                    "render": function ( data, type, full, meta ) {
                        var status = full["status"];
                        var stockName = full["name"];
                        var stockCode = full["code"];
                        var alert_price_low = full["alert_price_low"];
                        var alert_price_high = full["alert_price_high"];
                        if (status=="未上架"){
                             var cell = '<button class="button" type="text" onclick="update_putaway_for_vip(\'put_away\',\''+ stockCode + '\',\''+
                                 alert_price_low + '\',\''+ alert_price_high+ '\',\''+ stockName +'\')" >上架</button>';
                        }else{
                             var cell = '<button class="button" type="text" disabled="disabled" onclick="update_putaway_for_vip(\'put_away\',\''+ stockCode + '\',\''+
                                 alert_price_low + '\',\''+ alert_price_high+ '\',\''+ stockName +'\')" >上架</button>';
                        }
                        return cell;
                    }
                },
                {
                    "targets": -2,
                    "render": function ( data, type, full, meta ) {
                        var status = full["status"];
                        var stockCode = full["code"];
                        var stockName = full["name"];
                        var begin_date = full["begin_date"];
                        var putAwayPrice = full["put_away_price"];
                        if (status=="已上架"){
                         var cell = '<button class="button" type="text" ' +
                         'onclick="update_soldout_for_vip(\'sold_out\',\''+ stockCode +'\',\''+
                         stockName +'\',\''+putAwayPrice +'\',\''+ begin_date +'\')" >下架</button>';
                        }else{
                         var cell = '<button class="button" type="text" disabled="disabled" ' +
                         'onclick="update_soldout_for_vip(\'sold_out\',\''+ stockCode +'\',\''+
                         stockName +'\',\''+putAwayPrice +'\',\''+ begin_date +'\')" >下架</button>';
                        }
                        return cell;
                    }
                },
                {
                    "targets": -1,
                    "render": function ( data, type, full, meta ) {
                        var stockCode = full["code"];
                        var beginDate = full["begin_date"];
                        var cell = '<button class="stock_for_vip button ' + stockCode  + ' ' + beginDate +
						 '" type="text" onclick="editor_stock_for_vip(\''+ stockCode + '\',\''+ beginDate +'\')" >编辑</button>';
                        return cell;
                    }
                }
                ]
            });
            getTradeDetailTable("running",runningUserInfoList);
            getTradeDetailTable("over",overUserInfoList);
            if (userDataTable !=null){
                userDataTable.destroy();
            }
            userDataTable = $('#userDataTable').DataTable( {
                //"paging":false,
                "ordering": true,
                "info":     false,
                "data": userList,
                "showRowNumber":true,
                "lengthChange": false,
                "columns": [
                    {"data":"row_num"},
                    { "data": "account_id" },
                    { "data": "password" },
                    {"data":"registration_time"},
                    {"data":"inviter"},
                    { "data": "contact_way" },
                    {"data":"client_info"},
                    { "data": "login_count" },
                    {"data":"invitation_code"},
                    {"data":"user_type"},
                    {"data":"pay_money"},
                    {"data":"invite_people_num"},
                    {"data":"pay_people_num"},
                    {"data":"reward_money"},
                    {"data":"reward_period"},
                    {"data":"reward_remark"},
                    {"data":"remaining_times"},
                    {"data":""},
                    {"data":""},
                    {"data":""},
                    {"data":""}
                ],
                "columnDefs": [
                {
                    "targets": -1,
                    "render": function ( data, type, full, meta ) {
                        var account_id = full["account_id"];
                        var password = full["password"];
                        var cell = '<button class="button ' + account_id + ' '+ password +
						 '" type="text" onclick="del_user(\''+ account_id + '\',\''+ password +'\')" >删除</button>';
                        return cell;
                    }
                },
                {
                    "targets": -2,
                    "render": function ( data, type, full, meta ) {
                        var account_id = full["account_id"];
                        var password = full["password"];
                        var cell = '<button class="button ' + account_id + ' '+ password +
						 '" type="text" onclick="editor_user(\''+ account_id + '\',\''+ password +'\')" >编辑</button>';
                        return cell;
                    }
                },
                {
                    "targets": -3,
                    "render": function ( data, type, full, meta ) {
                        var account_id = full["account_id"];
                        var password = full["password"];
                        var client_info = full["client_info"];
                        var cell = '<button class="button ' + account_id + ' '+ password +
						 '" type="text" onclick="check_detail(\''+ client_info +'\')" >查看</button>';
                        return cell;
                    }
                },
                {
                    "targets": -4,
                    "render": function ( data, type, full, meta ) {
                        var account_id = full["account_id"];
                        var password = full["password"];
                        var remaining_times = full["remaining_times"];
                        if (remaining_times > 0){
                            {#var cell = '<button class="button ' + account_id + ' '+ password +#}
                            {# '" type="text" onclick="change_account_status(\''+ account_id+ '\',\''+account_status +'\')" >正常</button>';#}
                            var cell = '正常';
                        }else{
                            {#var cell = '<button class="button ' + account_id + ' '+ password +#}
                            {# '" type="text" onclick="change_account_status(\''+ account_id + '\',\''+account_status+'\')" >停用</button>';#}
                            var cell = '停用';
                        }
                        return cell;
                    }
                }
                ]
            } );
            if (messageHistoryDataTable !=null){
                messageHistoryDataTable.destroy();
            }
            messageHistoryDataTable = $('#messageHistoryDataTable').DataTable(  {
                "ordering": false,
                "info":     false,
                "data": messageList,
                "showRowNumber":true,
                "lengthChange": false,
                "columns": [
                    {"data":"id"},
                    {"data":"datetime"},
                    { "data": "title" },
                    { "data": "type" },
                    {"data":"message"},
                    {"data":""}
                ],
                "columnDefs": [
                    {
                        "targets": -1,
                        "render": function ( data, type, full, meta ) {
                            var id = full["id"];
                            var cell = '<button class="button" type="text" onclick="del_history_message(\''+ id  +'\')" >删除</button>';
                            return cell;
                        }
                    }
                ]
            });
            var htmlStr = "";
            var endDateList = [];
            for (var i=0;i<resultList.length;i++){
                var endDate = resultList[i]["end_date"];
                if (endDate!="" & endDateList.indexOf(endDate) == -1){
                    htmlStr +=
                        "<label class=\"checkbox-inline\">" +
                        "<input class=\"end-date-checkbox\" type=\"checkbox\" id=\"inlineCheckbox1\" value=\"" + endDate +
                        "\">" + endDate +
                        "</label>";
                }
                endDateList.push(endDate);
            }
            $("#end_date_checkbox").html(htmlStr);

            changeDisplay('stock');
        }
    });
}

function refreshDataForTable() {
    $.ajax({
        url: "/stockIncubatorData",
        async: true,
        success: function (result) {
            if (jQuery.type(result)=="string"){
                var obj = JSON.parse(result);
            }else{
                var obj = result;
            }
            resultList = obj.resultList;
            userList = obj.userList;
            userInfoList = obj.userInfoList;
            var runningUserInfoList = obj.runningUserInfoList;
            var overUserInfoList = obj.overUserInfoList;
            var totalProfit = obj.totalProfit;
            var messageList = obj.messageList;
            var vipList = obj.vipList;
            stockManageDataTable.data(resultList, 'stock_code');
            if (payUserStockDataTable !=null){
                payUserStockDataTable.destroy();
            }
            payUserStockDataTable = $('#payUserStockDataTable').DataTable({
                "ordering": false,
                "info":     false,
                "data": vipList,
                "lengthChange": false,
                "columns": [
                    {"data":"id"},
                    { "data": "name" },
                    { "data": "code" },
                    {"data":"begin_date"},
                    {"data":"begin_time"},
                    {"data":"put_away_price"},
                    {"data":"end_date"},
                    {"data":"end_time"},
                    {"data":"sold_out_price"},
                    { "data": "profit" },
                    {"data":"status"},
                    { "data": "alert_price_low" },
                    {"data":"alert_price_high"},
                    {"data":"client_info"},
                    {"data":""},
                    {"data":""},
                    {"data":""}
                ],
                "columnDefs": [
                    /*
                {
                    "targets": 7,
                    "render": function ( data, type, full, meta ) {
                        var stockCode = full["code"];
                        var beginDate = full["begin_date"];
                        var htmlStr = '<input style="width:80px" class="stock_for_vip profit '+ stockCode +' '+beginDate+
						 '" type="text"  disabled=true  value="'+
						 data + '" />';
                        return htmlStr;
                    }
                },*/
                {
                    "targets": 11,
                    "render": function ( data, type, full, meta ) {
                        var stockCode = full["code"];
                        var beginDate = full["begin_date"];
                        var htmlStr = '<input style="width:80px" class="stock_for_vip alert_price_low '+ stockCode +' '+beginDate+
						 '" type="text"  disabled=true  value="'+
						 data + '" />';
                        return htmlStr;
                    }
                },
                {
                    "targets": 12,
                    "render": function ( data, type, full, meta ) {
                        var stockCode = full["code"];
                        var beginDate = full["begin_date"];
                        var htmlStr = '<input style="width:80px" class="stock_for_vip alert_price_high '+ stockCode +' '+beginDate+
						 '" type="text"  disabled=true  value="'+
						 data + '" />';
                        return htmlStr;
                    }
                },
                {
                    "targets": -3,
                    "render": function ( data, type, full, meta ) {
                        var status = full["status"];
                        var stockName = full["name"];
                        var stockCode = full["code"];
                        var alert_price_low = full["alert_price_low"];
                        var alert_price_high = full["alert_price_high"];
                        if (status=="未上架"){
                             var cell = '<button class="button" type="text" onclick="update_putaway_for_vip(\'put_away\',\''+ stockCode + '\',\''+
                                 alert_price_low + '\',\''+ alert_price_high+ '\',\''+ stockName +'\')" >上架</button>';
                        }else{
                             var cell = '<button class="button" type="text" disabled="disabled" onclick="update_putaway_for_vip(\'put_away\',\''+ stockCode + '\',\''+
                                 alert_price_low + '\',\''+ alert_price_high+ '\',\''+ stockName +'\')" >上架</button>';
                        }
                        return cell;
                    }
                },
                {
                    "targets": -2,
                    "render": function ( data, type, full, meta ) {
                        var status = full["status"];
                        var stockCode = full["code"];
                        var stockName = full["name"];
                        var begin_date = full["begin_date"];
                        var putAwayPrice = full["put_away_price"];
                        if (status=="已上架"){
                         //var cell = '<button class="button" type="text" onclick="update_soldout_for_vip(\'sold_out\',\''+ stockCode +'\',\''+ begin_date +'\')" >下架</button>';
                         var cell = '<button class="button" type="text" ' +
                         'onclick="update_soldout_for_vip(\'sold_out\',\''+ stockCode +'\',\''+
                         stockName +'\',\''+putAwayPrice +'\',\''+ begin_date +'\')" >下架</button>';

                        }else{
                         var cell = '<button class="button" type="text" disabled="disabled" ' +
                             'onclick="update_soldout_for_vip(\'sold_out\',\''+ stockCode +'\',\''+
                             stockName +'\',\''+putAwayPrice +'\',\''+ begin_date +'\')" >下架</button>';
                        }
                        return cell;
                    }
                },
                {
                    "targets": -1,
                    "render": function ( data, type, full, meta ) {
                        var stockCode = full["code"];
                        var beginDate = full["begin_date"];
                        var cell = '<button class="stock_for_vip button ' + stockCode  + ' ' + beginDate +
						 '" type="text" onclick="editor_stock_for_vip(\''+ stockCode + '\',\''+ beginDate +'\')" >编辑</button>';
                        return cell;
                    }
                }
                ]
            });
            getTradeDetailTable("running",runningUserInfoList);
            getTradeDetailTable("over",overUserInfoList);
            if (userDataTable !=null){
                userDataTable.destroy();
            }
            userDataTable = $('#userDataTable').DataTable( {
                //"paging":false,
                "ordering": false,
                "info":     false,
                "data": userList,
                "showRowNumber":true,
                "lengthChange": false,
                "columns": [
                    {"data":"row_num"},
                    { "data": "account_id" },
                    { "data": "password" },
                    {"data":"registration_time"},
                    {"data":"inviter"},
                    { "data": "contact_way" },
                    {"data":"client_info"},
                    { "data": "login_count" },
                    {"data":"invitation_code"},
                    {"data":"user_type"},
                    {"data":"pay_money"},
                    {"data":"invite_people_num"},
                    {"data":"pay_people_num"},
                    {"data":"reward_money"},
                    {"data":"reward_period"},
                    {"data":"reward_remark"},
                    {"data":"remaining_times"},
                    {"data":""},
                    {"data":""},
                    {"data":""},
                    {"data":""}
                ],
                "columnDefs": [
                {
                    "targets": -1,
                    "render": function ( data, type, full, meta ) {
                        var account_id = full["account_id"];
                        var password = full["password"];
                        var cell = '<button class="button ' + account_id + ' '+ password +
						 '" type="text" onclick="del_user(\''+ account_id + '\',\''+ password +'\')" >删除</button>';
                        return cell;
                    }
                },
                {
                    "targets": -2,
                    "render": function ( data, type, full, meta ) {
                        var account_id = full["account_id"];
                        var password = full["password"];
                        var cell = '<button class="button ' + account_id + ' '+ password +
						 '" type="text" onclick="editor_user(\''+ account_id + '\',\''+ password +'\')" >编辑</button>';
                        return cell;
                    }
                },
                {
                    "targets": -3,
                    "render": function ( data, type, full, meta ) {
                        var account_id = full["account_id"];
                        var password = full["password"];
                        var client_info = full["client_info"];
                        var cell = '<button class="button ' + account_id + ' '+ password +
						 '" type="text" onclick="check_detail(\''+ client_info +'\')" >查看</button>';
                        return cell;
                    }
                },
                {
                    "targets": -4,
                    "render": function ( data, type, full, meta ) {
                        var account_id = full["account_id"];
                        var password = full["password"];
                        var remaining_times = full["remaining_times"];
                        if (remaining_times > 0){
                            {#var cell = '<button class="button ' + account_id + ' '+ password +#}
                            {# '" type="text" onclick="change_account_status(\''+ account_id+ '\',\''+account_status +'\')" >正常</button>';#}
                            var cell = '正常';
                        }else{
                            {#var cell = '<button class="button ' + account_id + ' '+ password +#}
                            {# '" type="text" onclick="change_account_status(\''+ account_id + '\',\''+account_status+'\')" >停用</button>';#}
                            var cell = '停用';
                        }
                        return cell;
                    }
                }
                ]
            } );
            if (messageHistoryDataTable !=null){
                messageHistoryDataTable.destroy();
            }
            messageHistoryDataTable = $('#messageHistoryDataTable').DataTable(  {
                "ordering": false,
                "info":     false,
                "data": messageList,
                "showRowNumber":true,
                "lengthChange": false,
                "columns": [
                    {"data":"id"},
                    {"data":"datetime"},
                    { "data": "title" },
                    { "data": "type" },
                    {"data":"message"},
                    {"data":""}
                ],
                "columnDefs": [
                    {
                        "targets": -1,
                        "render": function ( data, type, full, meta ) {
                            var id = full["id"];
                            var cell = '<button class="button" type="text" onclick="del_history_message(\''+ id  +'\')" >删除</button>';
                            return cell;
                        }
                    }
                ]
            });
            selectSource($("#stock").text());
            selectSourceForUser($("#user-type-button").text());
        }
    });
}
function del_history_message(id) {
    $.ajax({
        url:"/del_history_message",
        async:true,
        type:'POST',
        data:JSON.stringify({"id":id}),
        dataType:'json',
        success:function (result) {
            var message = result.message;
            alert(message);
            refreshDataForTable();
        }
    })
}
function realtimeRefreshDataForTable() {
    $.ajax({
        url: "/realtimeRefreshTradeDetailData",
        async: true,
        success: function (result) {
            if (jQuery.type(result)=="string"){
                var obj = JSON.parse(result);
            }else{
                var obj = result;
            }
            var runningUserInfoList = obj.runningUserInfoList;
            if (tradeDetailRunningDataTable !=null){
                for (var i=0;i<runningUserInfoList.length;i++){
                    var id = runningUserInfoList[i]["id"];
                    if ($(".trade_detail.button." + id).text() == "提交"){
                        return;
                    }
                }
            }
            getTradeDetailTable("running",runningUserInfoList);
        }
    });
}

function refreshPayUserCheckBox(){
    /*
    $.getJSON("/static/json/on_line_pay_user.json",function (result) {
        if ($.type(result)=="string"){
            result = JSON.parse(result);
        }
        if (result.length==0){
            alert("没有付费用户在线!!");
            return;
        }
        var htmlStr = "";
        for (var i=0;i<result.length;i++){
            var account_id = result[i]["account_id"];
            var client_info = result[i]["client_info"];
            if (client_info !=""){
                htmlStr +=
                "<label class=\"checkbox-inline\">" +
                "<input class=\"pay-user-checkbox\" type=\"checkbox\" id=\"inlineCheckbox1\" value=\"" + account_id +
                "\">" + client_info +
                "</label>";
            }

        }

        $("#pay_user_checkbox").html(htmlStr);
        document.getElementById('vip_checkbox').style.display='block';
    });*/
    $.ajax({
            url:"payUserOnLine",
            async:true,
            success:function(result){
            if ($.type(result)=="string"){
                result = JSON.parse(result);
            }
            if (result.length==0){
                alert("没有付费用户在线!!");
                return;
            }
            var htmlStr = "";
            for (var i=0;i<result.length;i++){
                var account_id = result[i]["account_id"];
                var client_info = result[i]["client_info"];
                if (client_info !=""){
                    htmlStr +=
                    "<label class=\"checkbox-inline\">" +
                    "<input class=\"pay-user-checkbox\" type=\"checkbox\" id=\"inlineCheckbox1\" value=\"" + account_id +
                    "\">" + client_info +
                    "</label>";
                }
                }
            $("#pay_user_checkbox").html(htmlStr);
            document.getElementById('vip_checkbox').style.display='block';
        }});

}

function changeDisplay(type){

$("#stock").css("background-color","white");
$("#stock-toggle").css("background-color","white");
$("#user").css("background-color","white");
$("#editor").css("background-color","white");
$("#message").css("background-color","white");
$("#trade").css("background-color","white");
var setColor="rgb("+68+","+113+","+188+")";
$("#"+type).css("background-color",setColor);

$("#stock-manage").hide();
$("#client-manage").hide();
$("#trade-detail").hide();
$("#pop_up_window_editor").hide();
$("#message_history").hide();
if (type=='stock'){
    $("#stock-toggle").css("background-color",setColor);
    $("#stock-manage").show();
    changeDisplayForStock("free-user");
}
if (type=='user'){
    $("#client-manage").show();
}
if (type=="trade"){
    $("#trade-detail").show();
    changeDisplayForTradeDetail("running");
}
if (type=='editor'){
    $("#pop_up_window_editor").show();
}
if (type=='message')
{
    $("#message_history").show();
}
}

function changeDisplayForStock(type){
$("#free-user").css("background-color","white");
$("#pay-user").css("background-color","white");
var setColor="rgb("+68+","+113+","+188+")";
$("#"+type).css("background-color",setColor);
$("#free-user-stock-manage").hide();
$("#pay-user-stock-manage").hide();
if (type=="free-user"){
    $("#free-user-stock-manage").show();
}
if (type=="pay-user"){
    $("#pay-user-stock-manage").show();
}
}

function  changeDisplayForTradeDetail(type){
    $("#running").css("background-color","white");
    $("#over").css("background-color","white");
    var setColor="rgb("+68+","+113+","+188+")";
    $("#"+type).css("background-color",setColor);
    $("#tradeDetailRunningDataTableDiv").hide();
    $("#tradeDetailOverDataTableDiv").hide();
    if (type == "running"){
        $("#tradeDetailRunningDataTableDiv").show();
    }
    if (type == "over"){
        $("#tradeDetailOverDataTableDiv").show();
    }
}

function tradeDetailDataTableAddRow(){

    tradeDetailDataTable.row.add({
        "id":userInfoList.length+1,
        "client_info":"",
        "contact_way":"",
        "buy_time":"",
        "stock_name":"",
         "put_away_time" :"",
        "sold_out_time":"",
         "first_cost" :"",
        "current_price":"",
        "change_ratio":"",
        "payback_cost":"",
        "trade_status":"",
        "remaining_times":""
    }).draw();
}

refreshData();
changeDisplay('stock');
setInterval(realtimeRefreshDataForTable,8000);
</script>

</body>
</html>